package com.yimi.appstore.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.yimi.appstore.db.AppInfo;
import com.yimi.appstore.db.ModeManagerBase;
import com.yimi.appstore.util.Util;

public class AppInfoEntityManager extends ModeManagerBase {

	private static AppInfoEntityManager AInstance;

	private AppInfoEntityManager() {

	}

	public static AppInfoEntityManager getInstance() {
		if (AInstance == null)
			AInstance = new AppInfoEntityManager();
		return AInstance;
	}

	public void close() {
		_db.close();
	}

	private ContentValues getValues(AppInfo appInfo) {
		ContentValues values = new ContentValues();
		values.put(AppInfoEntity.APP_NAME, appInfo.getName());
		values.put(AppInfoEntity.APP_PACKAGE_NAME, appInfo.getPackageName());
		values.put(AppInfoEntity.APP_SIZE, appInfo.getSize());
		values.put(AppInfoEntity.APP_SOURCE_DIR, appInfo.getSourceDir());
		values.put(AppInfoEntity.APP_VERSION_CODE, appInfo.getVersionCode());
		values.put(AppInfoEntity.APP_VERSION_NAME, appInfo.getVersionName());
		values.put(AppInfoEntity.APP_TARNS_FLAG, appInfo.getTransFlag());
		values.put(AppInfoEntity.APP_SYSTEM_FALG, appInfo.getSysFlag());
		return values;
	}

	/**
	 * 获取所有应用
	 * 
	 * @return
	 */
	public List<AppInfo> getAllApps() {
		List<AppInfo> result = new ArrayList<AppInfo>();
		Cursor cursor = _db.query(AppInfoEntity.APPINFO_TABLE_NAME, null, null,
				null, null, null, null, null);
		while (cursor.moveToNext()) {
			AppInfo app = new AppInfo(cursor.getInt(cursor
					.getColumnIndex(AppInfoEntity._ID)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_PACKAGE_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_VERSION_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_VERSION_CODE)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_SOURCE_DIR)),
					cursor.getInt((cursor
							.getColumnIndex(AppInfoEntity.APP_SIZE))),
					cursor.getInt(cursor
							.getColumnIndex(AppInfoEntity.APP_TARNS_FLAG)),
					cursor.getInt(cursor
							.getColumnIndex(AppInfoEntity.APP_SYSTEM_FALG)),
							Util.getPictureDraw(cursor.getBlob(cursor
							.getColumnIndex(AppInfoEntity.APP_ICON))));
			result.add(app);
		}
		cursor.close();
		return result;
	}

	/**
	 * 获取所有本地应用
	 * 
	 * @return
	 */
	public List<AppInfo> getAllLocalApps() {
		List<AppInfo> result = new ArrayList<AppInfo>();
		final String selectArgs[] = { "0" };
		Cursor cursor = _db.query(AppInfoEntity.APPINFO_TABLE_NAME, null,
				AppInfoEntity.APP_SYSTEM_FALG + " = ?", selectArgs, null, null,
				null);

		while (cursor.moveToNext()) {
			AppInfo app = new AppInfo(cursor.getInt(cursor
					.getColumnIndex(AppInfoEntity._ID)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_PACKAGE_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_VERSION_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_VERSION_CODE)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_SOURCE_DIR)),
					cursor.getInt((cursor
							.getColumnIndex(AppInfoEntity.APP_SIZE))),
					cursor.getInt(cursor
							.getColumnIndex(AppInfoEntity.APP_TARNS_FLAG)),
					cursor.getInt(cursor
							.getColumnIndex(AppInfoEntity.APP_SYSTEM_FALG)),
							Util.getPictureDraw(cursor.getBlob(cursor
							.getColumnIndex(AppInfoEntity.APP_ICON))));
			result.add(app);
		}
		cursor.close();
		return result;
	}

	/**
	 * 获取所有可以移动到sdk的应用
	 * 
	 * @return
	 */
	public List<AppInfo> getAllMovedApps() {
		List<AppInfo> result = new ArrayList<AppInfo>();
		final String selectArgs[] = { "0" };
		Cursor cursor = _db.query(AppInfoEntity.APPINFO_TABLE_NAME, null,
				AppInfoEntity.APP_TARNS_FLAG + " = ?", selectArgs, null, null,
				null);

		while (cursor.moveToNext()) {
			AppInfo app = new AppInfo(cursor.getInt(cursor
					.getColumnIndex(AppInfoEntity._ID)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_PACKAGE_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_VERSION_NAME)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_VERSION_CODE)),
					cursor.getString(cursor
							.getColumnIndex(AppInfoEntity.APP_SOURCE_DIR)),
					cursor.getInt((cursor
							.getColumnIndex(AppInfoEntity.APP_SIZE))),
					cursor.getInt(cursor
							.getColumnIndex(AppInfoEntity.APP_TARNS_FLAG)),
					cursor.getInt(cursor
							.getColumnIndex(AppInfoEntity.APP_SYSTEM_FALG)),
							Util.getPictureDraw(cursor.getBlob(cursor
							.getColumnIndex(AppInfoEntity.APP_ICON))));
			result.add(app);
		}
		cursor.close();
		return result;

	}

	/**
	 * 添加应用
	 * 
	 * @param appInfo
	 * @return
	 */
	public int addApp(AppInfo appInfo) {
		return (int) _db.insert(AppInfoEntity.APPINFO_TABLE_NAME, null,
				getValues(appInfo));
	}
	
	/**
	 * 删除应用
	 * @param packageName
	 * @return
	 */
	public int deleteApp(String packageName){
		return (int)_db.delete(AppInfoEntity.APPINFO_TABLE_NAME, AppInfoEntity.APP_PACKAGE_NAME + " = ?", new String[]{packageName});
	}
	
	/**
	 * 更新应用
	 * @param appInfo
	 * @return
	 */
	public int updataApp(AppInfo appInfo){
		return (int) _db.update(AppInfoEntity.APPINFO_TABLE_NAME, getValues(appInfo), AppInfoEntity.APP_PACKAGE_NAME +" = ?", new String[]{appInfo.getPackageName()});
	}
	
	/**
	 * 更新应用 Transflg
	 * @param appInfo
	 * @return
	 */
	public int updataAppTransflg(String pckName , int flags){
		ContentValues values = new ContentValues();
		values.put(AppInfoEntity.APP_TARNS_FLAG, flags);
		return (int) _db.update(AppInfoEntity.APPINFO_TABLE_NAME, values, AppInfoEntity.APP_PACKAGE_NAME +" = ?", new String[]{""+pckName});
	}

}
